package com.jhd.jiangxi.util;

import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import com.jhd.jiangxi.R;
import com.jhd.jiangxi.activity.main.BaseActivity;
import com.jhd.jiangxi.vo.PiatsInfo;

import net.zigin.commons.util.StringUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class TextDialog {
    private static AlertDialog _alertDialog=null;
    private static long lastClickTime=0;

    public static void showDialog(Context context, List<PiatsInfo> piatsInfos,int position,TextView textView){
        long now = System.currentTimeMillis();
        if(now - lastClickTime>3000){
            lastClickTime = now;
            LayoutInflater mLayoutInflater = LayoutInflater.from(context);
            View view = mLayoutInflater.inflate(R.layout.dialog_edittext, null);
            final AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context, R.style.ActionSheetDialogStyle);
            alertBuilder.setView(view);
            _alertDialog = alertBuilder.create();
            //获取当前Activity所在的窗体
            Window window = _alertDialog.getWindow();
            window.setGravity(Gravity.CENTER);
            if(_alertDialog.isShowing())_alertDialog.dismiss();
            _alertDialog.show();
            EditText et_edit = (EditText) view.findViewById(R.id.et_edit);
            ImageView iv_clean = (ImageView) view.findViewById(R.id.iv_clean);
            Button btn_confirm = (Button) view.findViewById(R.id.btn_confirm);
            et_edit.setFocusable(true);
            et_edit.setFocusableInTouchMode(true);
            et_edit.requestFocus();
            et_edit.setText(textView.getText().toString());
            if(!et_edit.getText().toString().equals("")){
                iv_clean.setVisibility(View.VISIBLE);
            }
            iv_clean.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    et_edit.setText("");
                    iv_clean.setVisibility(View.GONE);
                }
            });
            et_edit.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    if(!s.toString().equals("")){
                        iv_clean.setVisibility(View.VISIBLE);
                    }else{
                        iv_clean.setVisibility(View.GONE);
                    }
                }
            });
            btn_confirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(et_edit.getText().toString().trim().equals("")||et_edit.getText().toString().trim().equals("0")){
                        Toast.makeText(context,"数量必须填入正数!",Toast.LENGTH_SHORT).show();
                    }else{
                        int num= Integer.parseInt(piatsInfos.get(position).getPkgAmount());
                        if(Integer.parseInt(et_edit.getText().toString().trim())>num){
                            Toast.makeText(context,"数量不能大于包装量!",Toast.LENGTH_SHORT).show();
                        }else{
                            _alertDialog.dismiss();
                            textView.setText(et_edit.getText().toString());
                            piatsInfos.get(position).setWriteQuality(et_edit.getText().toString().trim());
                        }
                    }
                }
            });

            WindowManager.LayoutParams params = window.getAttributes() ;
            params.width =(int) (((Activity)context).getWindowManager().getDefaultDisplay().getWidth()*0.9);                     //使用这种方式更改了dialog的框宽
            window.setAttributes(params);
        }
    }
}
